﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EventRegistration;


namespace Login
{
    public class CheckLoginInfor
    {

        public int validating(string str1, string str2) //Check and compare data from user input and database
        {
            ConnectUserDataBase loginuse = new ConnectUserDataBase(); 
            DataTable table = new DataTable(); //Create the datatable
            table = loginuse.laydulieu(); //Get data using custom function

            foreach (DataRow row in table.Rows)
            {
                if (((row[0].ToString()).CompareTo(str1) == 0) && ((row[1].ToString()).CompareTo(str2) == 0)) //Find and Compare Account in Database
                {
                    if (str1.ToLower() == "admin" && str2.ToLower() == "adminstrator")
                    {
                        CurrentData.RegistrationInfo.Completed = true;
                        return 2;
                    }
                    else
                    {
                        CurrentData.RegistrationInfo.Completed = true;
                        return 1;
                    }
                }
            }
            return 0;


        }
    }

    public class ConnectUserDataBase //Get data from User Database
    {
        public DataTable laydulieu()
        {
            DataTable dt = new DataTable();
            SqlConnection sqlcon = new SqlConnection();
            string url = ConfigurationManager.ConnectionStrings["SqlProviderConUserData"].ToString();
            using (SqlConnection con = new SqlConnection(url))
            {
                using (SqlDataAdapter da = new SqlDataAdapter())
                {
                    string sql = "select [UserName], [PassWord] from [CocShopUserInfor].[dbo].[UserInfor]";
                    da.SelectCommand = new SqlCommand(sql, con);

                    da.Fill(dt);
                    return dt;

                }
            }
        }
    }

    public partial class LoginForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Check if registration info is completed, if that show the logged in text
            if (CurrentData.RegistrationInfo.Completed)
            {
                loginButton.ClientEnabled = false;
                lbshow.Text = "You're already logged in";
            }
        }

        private void autoLogin(string username, string password) //Auto-Login
        {
            CheckLoginInfor chk = new CheckLoginInfor();
            chk.validating(username, password);
        }

        protected  void startOrderButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Schedule.aspx");
        }

        protected void loginButton_Click(object sender, EventArgs e)
        {
            CheckLoginInfor chk = new CheckLoginInfor();

            //Check the input if user login as administrator
            if ((chk.validating(txtusername.Text, txtpassword.Text)) == 2)
            {
                loginBigLabel.Text = "Administrator login";
                userNameLabel.Visible = false;
                passwordLabel.Visible = false;
                txtusername.Visible = false;
                txtpassword.Visible = false;
                loginButton.ClientVisible = false;
                startOrderButton.ClientVisible = true;
                return;

            }

            ////Check the input if user login as normal account
            if ((chk.validating(txtusername.Text, txtpassword.Text)) == 1)
            {
                loginBigLabel.Text = "Successfully!";
                userNameLabel.Visible = false;
                passwordLabel.Visible = false;
                txtusername.Visible = false;
                txtpassword.Visible = false;
                loginButton.ClientVisible = false;
                startOrderButton.ClientVisible = true;
                return;

            }

            //Check if login failed
            if ((chk.validating(txtusername.Text, txtpassword.Text)) == 0)
            {
                lbshow.Text = "Access Denied!";
                return;
            }
        }
    }
}





